%% @author Carlos Abalde <carlos.abalde@gmail.com>
%% @doc    ...

%%%----------------------------------------------------------------------
%%%
%%% EMC, Copyright (C) 2009 Carlos Abalde
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program; if not, write to the Free Software
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%----------------------------------------------------------------------

-module(emc.util.logger).
-behaviour(gen_server).

-import(gen_server).
-import(gen_event).
-import(lists).
-import(erlang).
-import(emc.util.config).
-import(emc.util.util).

%% API
-export([start_link/0, log/3]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-include("emc.hrl").

-define(EVENT_MANAGER, event_manager_logger).

-record(state, {}).

%%%-----------------------------------------------------------------------------
%%% API
%%%-----------------------------------------------------------------------------

%%
%% @doc starts event manager container/supervisor.
%%
start_link() ->
    util:start_link_gen_server({local, ?MODULE}, ?MODULE, [], []).

%%
%% @doc asynchronously logs an event.
%%
log(Source, Type, Data) ->
    Event = #event{ timestamp = now(),
		    pid       = self(),
		    source    = util:a2a(Source),
		    type      = util:a2a(Type),
		    data      = Data },
    gen_event:notify(?EVENT_MANAGER, Event).

%%%-----------------------------------------------------------------------------
%%% gen_server callbacks
%%%-----------------------------------------------------------------------------

%%
%%
%%
init(_) ->
    %% Init event manager.
    case gen_event:start_link({local, ?EVENT_MANAGER}) of
	{ok, _Pid} ->
	    %% Set event handlers
	    lists:foreach(fun({Handler, Opts}) ->
				  ok = gen_event:add_sup_handler(?EVENT_MANAGER, Handler, Opts)
			  end, config:get('emc.logger.event_handlers', []));
	{error,{already_started, _Pid}} ->
	    true
    end,
    {ok, #state{}}.

%%
%%
%%
handle_call(_, _, State) ->
    {reply, unknown_request, State}.

%%
%%
%%
handle_cast(_, State) ->
    {noreply, State}.

%%
%%
%%
%handle_info({gen_event_EXIT, Handler, Reason}, State) ->
%    {noreply, State};
    
handle_info(_, State) ->
    {noreply, State}.

%%
%%
%%
terminate(_, _State) ->
    %% Stop event manager
    catch gen_event:stop(?EVENT_MANAGER),
    ok.

%%
%%
%%
code_change(_, State, _) ->
    {ok, State}.

%%%-----------------------------------------------------------------------------
%%% Internal implementation
%%%-----------------------------------------------------------------------------
